package com.caipin.service.impl;

import com.caipin.service.AICourseHomePageManagementService;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-10-06 12:05
 * @Description: TODO
 */
@Service
public class AICourseHomePageManagementServiceImpl implements AICourseHomePageManagementService {
    @Autowired
    private CourseEventReportDao courseEventReportDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ClassroomDao classroomDao;

    @Autowired
    private CourseStatisticalRateDao courseStatisticalRateDao;

    @Autowired
    private ClassInvasionReportDao classInvasionReportDao;

    public List<AbnormalResultsRotationChart> getAbnormalResultsRotationChart() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.orderByDesc("create_time"))
                .last("limit 10");
        List<CourseEventReport> courseEventReportList = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (courseEventReportList == null || courseEventReportList.size() < 1)
            return null;
        List<String> studentIds = new ArrayList<>();
        for (CourseEventReport courseEventReport : courseEventReportList)
            studentIds.add(courseEventReport.getStudentId());
        List<Student> studentList = this.studentDao.selectBatchIds(studentIds);
        Map<String, Student> map = new HashMap<>();
        for (Student student : studentList)
            map.put(student.getId(), student);
        List<AbnormalResultsRotationChart> abnormalResultsRotationChartList = new ArrayList<>();
        for (CourseEventReport courseEventReport : courseEventReportList) {
            String path = courseEventReport.getImageSrc().replaceFirst("^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+:[0-9]+", "");
            abnormalResultsRotationChartList.add((new AbnormalResultsRotationChart())

                    .setEventName(courseEventReport.getAction())
                    .setImageSrc(path));
        }
        return abnormalResultsRotationChartList;
    }

    public List<AbnormalResultsClassroom> getAbnormalResultsClassroom() {
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Date todayStartDate = Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant());
        Date todayEndDate = Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant());
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.select(new String[] { "building", "classroom", "action", "COUNT(0) as actionCount" }).between("create_time", todayStartDate, todayEndDate))
                .groupBy((Object[])new String[] { "building", "classroom", "action" });
        List<Map<String, Object>> mapList = this.courseEventReportDao.selectMaps((Wrapper)queryWrapper);
        Map<String, List<AbnormalResultsClassroom.Event>> classroomEventMap = new HashMap<>();
        for (Map<String, Object> map : mapList) {
            String building = (String)map.get("building");
            String classroomId = (String)map.get("classroom");
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            ((QueryWrapper)queryWrapper1.eq("building_name", building))
                    .eq("classroom_id", classroomId);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            String action = (String)map.get("action");
            Long count = (Long)map.get("actionCount");
            List<AbnormalResultsClassroom.Event> eventList = classroomEventMap.get(classroom.getClassroomName());
            if (eventList == null)
                eventList = new ArrayList<>();
            eventList.add((new AbnormalResultsClassroom.Event())

                    .setNumber(Integer.valueOf(count.intValue()))
                    .setName(action));
            classroomEventMap.put(classroom.getClassroomName(), eventList);
        }
        List<AbnormalResultsClassroom> abnormalResultsClassroomList = new ArrayList<>();
        for (Map.Entry<String, List<AbnormalResultsClassroom.Event>> entry : classroomEventMap.entrySet())
            abnormalResultsClassroomList.add((new AbnormalResultsClassroom())

                    .setClassroomName(entry.getKey())
                    .setEventList(entry.getValue()));
        return abnormalResultsClassroomList;
    }

    public SchoolStatistics getStatisticsOfSchoolAttendanceForNow() {
        LocalDate today = LocalDate.now();
        LocalDateTime startLocalDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endLocalDateTime = LocalDateTime.of(today, LocalTime.MAX);
        Date startDate = Date.from(startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        SchoolStatistics statistics = new SchoolStatistics();
        statistics.setAttendanceRate(getStatisticsRate(startDate, endDate, ", true));
                statistics.setStandardRate(getStatisticsRate(startDate, endDate, ", true));
                        statistics.setConcentrationRate(getStatisticsRate(startDate, endDate, ", true));
                                statistics.setActivityRate(getStatisticsRate(startDate, endDate, ", true));
        return statistics;
    }

    public SchoolStatistics getStatisticsOfSchoolAttendanceForToday() {
        LocalDate today = LocalDate.now();
        LocalDateTime startLocalDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endLocalDateTime = LocalDateTime.of(today, LocalTime.MAX);
        Date startDate = Date.from(startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        SchoolStatistics statistics = new SchoolStatistics();
        statistics.setAttendanceRate(getStatisticsRate(startDate, endDate, "));
                statistics.setStandardRate(getStatisticsRate(startDate, endDate, "));
                        statistics.setConcentrationRate(getStatisticsRate(startDate, endDate, "));
                                statistics.setActivityRate(getStatisticsRate(startDate, endDate, "));
        return statistics;
    }

    public SchoolStatistics getStatisticsOfSchoolAttendanceForWeek() {
        LocalDate today = LocalDate.now();
        int weekValue = today.getDayOfWeek().getValue();
        LocalDateTime startLocalDateTime = LocalDateTime.of(today.minusDays((weekValue - 1)), LocalTime.MIN);
        LocalDateTime endLocalDateTime = LocalDateTime.of(today.plusDays((7 - weekValue)), LocalTime.MAX);
        Date startDate = Date.from(startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        SchoolStatistics statistics = new SchoolStatistics();
        statistics.setAttendanceRate(getStatisticsRate(startDate, endDate, "));
                statistics.setStandardRate(getStatisticsRate(startDate, endDate, "));
                        statistics.setConcentrationRate(getStatisticsRate(startDate, endDate, "));
                                statistics.setActivityRate(getStatisticsRate(startDate, endDate, "));
        return statistics;
    }

    public List<RankingTeacher> getRankingTeacherList() {
        RankingTeacher rankingTeacherAttendanceRate = (new RankingTeacher()).setEventName(").setInfos(getRankingTeacherListForEvent("));
        RankingTeacher rankingTeacherStandardRate = (new RankingTeacher()).setEventName(").setInfos(getRankingTeacherListForEvent("));
        RankingTeacher rankingTeacherConcentrationRate = (new RankingTeacher()).setEventName(").setInfos(getRankingTeacherListForEvent("));
        RankingTeacher rankingTeacherActivityRate = (new RankingTeacher()).setEventName(").setInfos(getRankingTeacherListForEvent("));
        List<RankingTeacher> rankingTeacherList = new ArrayList<>();
        rankingTeacherList.add(rankingTeacherAttendanceRate);
        rankingTeacherList.add(rankingTeacherStandardRate);
        rankingTeacherList.add(rankingTeacherConcentrationRate);
        rankingTeacherList.add(rankingTeacherActivityRate);
        return rankingTeacherList;
    }

    public List<RankingClbum> getRankingClbumList() {
        RankingClbum rankingClbumAttendanceRate = (new RankingClbum()).setEvent(").setInfos(getRankingClbumListForEvent("));
        RankingClbum rankingClbumStandardRate = (new RankingClbum()).setEvent(").setInfos(getRankingClbumListForEvent("));
        RankingClbum rankingClbumConcentrationRate = (new RankingClbum()).setEvent(").setInfos(getRankingClbumListForEvent("));
        RankingClbum rankingClbumActivityRate = (new RankingClbum()).setEvent(").setInfos(getRankingClbumListForEvent("));
        List<RankingClbum> rankingClbumList = new ArrayList<>();
        rankingClbumList.add(rankingClbumAttendanceRate);
        rankingClbumList.add(rankingClbumStandardRate);
        rankingClbumList.add(rankingClbumConcentrationRate);
        rankingClbumList.add(rankingClbumActivityRate);
        return rankingClbumList;
    }

    public List<InvasionWarning> getInvasionWarning(Integer count) {
        QueryWrapper<ClassInvasionReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.orderByDesc("create_time"))
                .last("LIMIT " + count);
        List<ClassInvasionReport> classInvasionReportList = this.classInvasionReportDao.selectList((Wrapper)queryWrapper);
        List<InvasionWarning> warningList = new ArrayList<>();
        for (ClassInvasionReport classInvasionReport : classInvasionReportList)
            warningList.add((new InvasionWarning())

                    .setGrade(classInvasionReport.getGrade())
                    .setClbum(classInvasionReport.getClbum())
                    .setHappenTime(classInvasionReport.getHappenTime())
                    .setImgSrc(classInvasionReport.getImgSrc()));
        return warningList;
    }

    public List<RaiseHandsStatistics> getRaiseHandsStatisticsForDay() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getDayStart(), getDayEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<RaiseHandsStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new RaiseHandsStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<RaiseHandsStatistics> collect = (List<RaiseHandsStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList());
        return collect;
    }

    public List<RaiseHandsStatistics> getRaiseHandsStatisticsForWeek() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getWeekStart(), getWeekEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<RaiseHandsStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new RaiseHandsStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<RaiseHandsStatistics> collect = (List<RaiseHandsStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList());
        return collect;
    }

    public List<RaiseHandsStatistics> getRaiseHandsStatisticsForMonth() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getMonthStart(), getMonthEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<RaiseHandsStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new RaiseHandsStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<RaiseHandsStatistics> collect = (List<RaiseHandsStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList());
        return collect;
    }

    public List<AttendanceStatistics> getAttendanceStatisticsForDay() {
        QueryWrapper<CourseStatisticalRate> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)queryWrapper.eq("event_name", "))
                .gt("percentage", Double.valueOf(80.0D)))
                .between("create_time", getDayStart(), getDayEnd());
        List<CourseStatisticalRate> courseStatisticalRates = this.courseStatisticalRateDao.selectList((Wrapper)queryWrapper);
        List<AttendanceStatistics> attendanceStatisticsList = new ArrayList<>();
        for (CourseStatisticalRate courseStatisticalRate : courseStatisticalRates)
            attendanceStatisticsList.add((new AttendanceStatistics())
                    .setClbum(courseStatisticalRate.getGrade() + "+ courseStatisticalRate.getClbum() + ")
                    .setRate(courseStatisticalRate.getPercentage().doubleValue()));
        Map<String, AttendanceStatisticsTemp> map = new HashMap<>();
        for (AttendanceStatistics attendanceStatistics : attendanceStatisticsList) {
            AttendanceStatisticsTemp attendanceStatisticsTemp = map.get(attendanceStatistics.getClbum());
            if (null == attendanceStatisticsTemp) {
                map.put(attendanceStatistics.getClbum(), (new AttendanceStatisticsTemp())
                        .setCount(1).setTotalRate(attendanceStatistics.getRate()));
                continue;
            }
            map.put(attendanceStatistics.getClbum(), attendanceStatisticsTemp

                    .setCount(attendanceStatisticsTemp.getCount() + 1)
                    .setTotalRate(attendanceStatisticsTemp.getTotalRate() + attendanceStatistics.getRate()));
        }
        List<AttendanceStatistics> result = new ArrayList<>();
        for (Map.Entry<String, AttendanceStatisticsTemp> entry : map.entrySet()) {
            String key = entry.getKey();
            AttendanceStatisticsTemp value = entry.getValue();
            result.add((new AttendanceStatistics()).setClbum(key).setRate(Math.round(value.getTotalRate() / value.getCount())));
        }
        List<AttendanceStatistics> collect = (List<AttendanceStatistics>)result.stream().sorted((o1, o2) -> {
            int val1 = (int)(o1.getRate() * 1000.0D);
            int val2 = (int)(o2.getRate() * 1000.0D);
            return val2 - val1;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<AttendanceStatistics> getAttendanceStatisticsForWeek() {
        QueryWrapper<CourseStatisticalRate> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)queryWrapper.eq("event_name", "))
                .gt("percentage", Double.valueOf(80.0D)))
                .between("create_time", getWeekStart(), getWeekEnd());
        List<CourseStatisticalRate> courseStatisticalRates = this.courseStatisticalRateDao.selectList((Wrapper)queryWrapper);
        List<AttendanceStatistics> attendanceStatisticsList = new ArrayList<>();
        for (CourseStatisticalRate courseStatisticalRate : courseStatisticalRates)
            attendanceStatisticsList.add((new AttendanceStatistics())
                    .setClbum(courseStatisticalRate.getGrade() + "+ courseStatisticalRate.getClbum() + ")
                    .setRate(courseStatisticalRate.getPercentage().doubleValue()));
        Map<String, AttendanceStatisticsTemp> map = new HashMap<>();
        for (AttendanceStatistics attendanceStatistics : attendanceStatisticsList) {
            AttendanceStatisticsTemp attendanceStatisticsTemp = map.get(attendanceStatistics.getClbum());
            if (null == attendanceStatisticsTemp) {
                map.put(attendanceStatistics.getClbum(), (new AttendanceStatisticsTemp())
                        .setCount(1).setTotalRate(attendanceStatistics.getRate()));
                continue;
            }
            map.put(attendanceStatistics.getClbum(), attendanceStatisticsTemp

                    .setCount(attendanceStatisticsTemp.getCount() + 1)
                    .setTotalRate(attendanceStatisticsTemp.getTotalRate() + attendanceStatistics.getRate()));
        }
        List<AttendanceStatistics> result = new ArrayList<>();
        for (Map.Entry<String, AttendanceStatisticsTemp> entry : map.entrySet()) {
            String key = entry.getKey();
            AttendanceStatisticsTemp value = entry.getValue();
            result.add((new AttendanceStatistics()).setClbum(key).setRate(Math.round(value.getTotalRate() / value.getCount())));
        }
        List<AttendanceStatistics> collect = (List<AttendanceStatistics>)result.stream().sorted((o1, o2) -> {
            int val1 = (int)(o1.getRate() * 1000.0D);
            int val2 = (int)(o2.getRate() * 1000.0D);
            return val2 - val1;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<AttendanceStatistics> getAttendanceStatisticsForMonth() {
        QueryWrapper<CourseStatisticalRate> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)queryWrapper.eq("event_name", "))
                .gt("percentage", Double.valueOf(80.0D)))
                .between("create_time", getMonthStart(), getMonthEnd());
        List<CourseStatisticalRate> courseStatisticalRates = this.courseStatisticalRateDao.selectList((Wrapper)queryWrapper);
        List<AttendanceStatistics> attendanceStatisticsList = new ArrayList<>();
        for (CourseStatisticalRate courseStatisticalRate : courseStatisticalRates)
            attendanceStatisticsList.add((new AttendanceStatistics())
                    .setClbum(courseStatisticalRate.getGrade() + "+ courseStatisticalRate.getClbum() + ")
                    .setRate(courseStatisticalRate.getPercentage().doubleValue()));
        Map<String, AttendanceStatisticsTemp> map = new HashMap<>();
        for (AttendanceStatistics attendanceStatistics : attendanceStatisticsList) {
            AttendanceStatisticsTemp attendanceStatisticsTemp = map.get(attendanceStatistics.getClbum());
            if (null == attendanceStatisticsTemp) {
                map.put(attendanceStatistics.getClbum(), (new AttendanceStatisticsTemp())
                        .setCount(1).setTotalRate(attendanceStatistics.getRate()));
                continue;
            }
            map.put(attendanceStatistics.getClbum(), attendanceStatisticsTemp

                    .setCount(attendanceStatisticsTemp.getCount() + 1)
                    .setTotalRate(attendanceStatisticsTemp.getTotalRate() + attendanceStatistics.getRate()));
        }
        List<AttendanceStatistics> result = new ArrayList<>();
        for (Map.Entry<String, AttendanceStatisticsTemp> entry : map.entrySet()) {
            String key = entry.getKey();
            AttendanceStatisticsTemp value = entry.getValue();
            result.add((new AttendanceStatistics()).setClbum(key).setRate(Math.round(value.getTotalRate() / value.getCount())));
        }
        List<AttendanceStatistics> collect = (List<AttendanceStatistics>)result.stream().sorted((o1, o2) -> {
            int val1 = (int)(o1.getRate() * 1000.0D);
            int val2 = (int)(o2.getRate() * 1000.0D);
            return val2 - val1;
        }).collect(Collectors.toList());
        return collect;
    }

    public List<WalkAroundStatistics> getWalkAroundStatisticsForDay() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getDayStart(), getDayEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<WalkAroundStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new WalkAroundStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<WalkAroundStatistics> collect = ((List<WalkAroundStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList())).subList(0, 5);
        return collect;
    }

    public List<WalkAroundStatistics> getWalkAroundStatisticsForWeek() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getWeekStart(), getWeekEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<WalkAroundStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new WalkAroundStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<WalkAroundStatistics> collect = ((List<WalkAroundStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList())).subList(0, 5);
        return collect;
    }

    public List<WalkAroundStatistics> getWalkAroundStatisticsForMonth() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getMonthStart(), getMonthEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<WalkAroundStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new WalkAroundStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<WalkAroundStatistics> collect = ((List<WalkAroundStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList())).subList(0, 5);
        return collect;
    }

    public List<SleepStatistics> getSleepStatisticsForDay() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getDayStart(), getDayEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<SleepStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new SleepStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<SleepStatistics> collect = ((List<SleepStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList())).subList(0, 5);
        return collect;
    }

    public List<SleepStatistics> getSleepStatisticsForWeek() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getWeekStart(), getWeekEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<SleepStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new SleepStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<SleepStatistics> collect = ((List<SleepStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList())).subList(0, 5);
        return collect;
    }

    public List<SleepStatistics> getSleepStatisticsForMonth() {
        QueryWrapper<CourseEventReport> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("create_time", getMonthStart(), getMonthEnd()))
                .eq("action", ");
                        List<CourseEventReport> eventReports = this.courseEventReportDao.selectList((Wrapper)queryWrapper);
        if (null == eventReports || eventReports.size() <= 0)
            return null;
        Map<String, Integer> map = new HashMap<>();
        for (CourseEventReport eventReport : eventReports) {
            Integer value = map.get(eventReport.getClassroom());
            if (null == value) {
                map.put(eventReport.getClassroom(), Integer.valueOf(1));
                continue;
            }
            map.put(eventReport.getClassroom(), Integer.valueOf(value.intValue() + 1));
        }
        List<SleepStatistics> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            QueryWrapper<Classroom> queryWrapper1 = new QueryWrapper();
            queryWrapper1.select(new String[] { "classroom_name" }).eq("classroom_id", key);
            Classroom classroom = (Classroom)this.classroomDao.selectOne((Wrapper)queryWrapper1);
            list.add((new SleepStatistics()).setClbum(classroom.getClassroomName()).setCount(value.intValue()));
        }
        List<SleepStatistics> collect = ((List<SleepStatistics>)list.stream().sorted((o1, o2) -> o2.getCount() - o1.getCount()).collect(Collectors.toList())).subList(0, 5);
        return collect;
    }

    private static Date getDayStart() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = now.with(LocalTime.MIN);
        Instant instant = start.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    private static Date getDayEnd() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = now.with(LocalTime.MAX);
        Instant instant = start.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    private static Date getWeekStart() {
        TemporalField fieldISO = WeekFields.of(Locale.getDefault()).dayOfWeek();
        LocalDate localDate = LocalDate.now();
        LocalDate start = localDate.with(fieldISO, 1L).plusDays(1L);
        LocalDateTime startTime = LocalDateTime.of(start, LocalTime.MIN);
        return Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    private static Date getWeekEnd() {
        TemporalField fieldISO = WeekFields.of(Locale.getDefault()).dayOfWeek();
        LocalDate localDate = LocalDate.now();
        LocalDate start = localDate.with(fieldISO, 7L).plusDays(1L);
        LocalDateTime startTime = LocalDateTime.of(start, LocalTime.MAX);
        return Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    private static Date getMonthStart() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        return Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
    }

    private static Date getMonthEnd() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
        return Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
    }

    private List<RankingClbum.Info> getRankingClbumListForEvent(String eventName) {
        LocalDate today = LocalDate.now();
        LocalDateTime startLocalDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endLocalDateTime = LocalDateTime.of(today, LocalTime.MAX);
        Date startDate = Date.from(startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        QueryWrapper<CourseStatisticalRate> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)((QueryWrapper)queryWrapper.select(new String[] { "grade", "clbum", "percentage" }).between("create_time", startDate, endDate))
                .eq("event_name", eventName))
                .orderByAsc("percentage"))
                .last("LIMIT 3");
        List<RankingClbum.Info> infoList = new ArrayList<>();
        List<Map<String, Object>> mapList = this.courseStatisticalRateDao.selectMaps((Wrapper)queryWrapper);
        for (Map<String, Object> map : mapList)
            infoList.add((new RankingClbum.Info())

                    .setGrade((String)map.get("grade"))
                    .setClbum((String)map.get("clbum"))
                    .setPercentage((Double)map.get("percentage")));
        return infoList;
    }

    private List<RankingTeacher.Info> getRankingTeacherListForEvent(String eventName) {
        LocalDate today = LocalDate.now();
        LocalDateTime startLocalDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endLocalDateTime = LocalDateTime.of(today, LocalTime.MAX);
        Date startDate = Date.from(startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
        QueryWrapper<CourseStatisticalRate> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)((QueryWrapper)queryWrapper.select(new String[] { "teacher_name AS teacher", "course_name AS course", "percentage" }).between("create_time", startDate, endDate))
                .eq("event_name", eventName))
                .orderByAsc("percentage"))
                .last("LIMIT 3");
        List<RankingTeacher.Info> infoList = new ArrayList<>();
        List<Map<String, Object>> mapList = this.courseStatisticalRateDao.selectMaps((Wrapper)queryWrapper);
        for (Map<String, Object> map : mapList)
            infoList.add((new RankingTeacher.Info())

                    .setCourseName((String)map.get("course"))
                    .setCourseTeacher((String)map.get("teacher"))
                    .setPercentage((Double)map.get("percentage")));
        return infoList;
    }

    private Double getStatisticsRate(Date startDate, Date endDate, String eventName, boolean now) {
        QueryWrapper<CourseStatisticalRate> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper
                .between("create_time", startDate, endDate))
                .eq("event_name", eventName);
        if (now) {
            ((QueryWrapper)queryWrapper.select(new String[] { "course_student_count AS studentTotal", "event_student_count AS eventTotal" }).orderByDesc("create_time"))
                    .last("limit 1");
        } else {
            queryWrapper.select(new String[] { "SUM(course_student_count) AS studentTotal", "SUM(event_student_count) AS eventTotal" });
        }
        List<Map<String, Object>> mapList = this.courseStatisticalRateDao.selectMaps((Wrapper)queryWrapper);
        if (mapList == null || mapList.isEmpty() || mapList.get(0) == null)
            return null;
        Iterator<Map<String, Object>> iterator = mapList.iterator();
        if (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            Long studentTotal = null;
            Long eventTotal = null;
            if (now) {
                studentTotal = Long.valueOf(((Integer)map.get("studentTotal")).longValue());
                eventTotal = Long.valueOf(((Integer)map.get("eventTotal")).longValue());
            } else {
                studentTotal = Long.valueOf(((BigDecimal)map.get("studentTotal")).longValue());
                eventTotal = Long.valueOf(((BigDecimal)map.get("eventTotal")).longValue());
            }
            double v = eventTotal.doubleValue() / studentTotal.doubleValue();
            return Double.valueOf((new BigDecimal(v * 100.0D)).setScale(2, RoundingMode.UP).doubleValue());
        }
        return Double.valueOf(0.0D);
    }

    private Double getStatisticsRate(Date startDate, Date endDate, String eventName) {
        return getStatisticsRate(startDate, endDate, eventName, false);
    }
}
